<?php

/**
 * This is the model class for table "joke_content".
 *
 * The followings are the available columns in table 'joke_content':
 * @property string $id
 * @property string $created_on
 * @property string $title
 * @property string $text
 * @property integer $valid
 * @property string $category_id
 * @property integer $grades_number
 * @property integer $grades_total
 * @property string $last_modified_on
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property JokeCategories $category
 * @property Users $user
 */
class JokeContent extends CActiveRecord {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return JokeContent the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'joke_content';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('created_on, title, text, category_id, grades_number, grades_total, last_modified_on, user_id', 'required'),
			array('valid, grades_number, grades_total, user_id', 'numerical', 'integerOnly' => true),
			array('title', 'length', 'max' => 250),
			array('category_id', 'length', 'max' => 20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, created_on, title, text, valid, category_id, grades_number, grades_total, last_modified_on, user_id', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'JokeCategories', 'category_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'created_on' => 'Created On',
			'title' => 'Title',
			'text' => 'Text',
			'valid' => 'Valid',
			'category_id' => 'Category',
			'grades_number' => 'Grades Number',
			'grades_total' => 'Grades Total',
			'last_modified_on' => 'Last Modified On',
			'user_id' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id, true);
		$criteria->compare('created_on', $this->created_on, true);
		$criteria->compare('title', $this->title, true);
		$criteria->compare('text', $this->text, true);
		$criteria->compare('valid', $this->valid);
		$criteria->compare('category_id', $this->category_id, true);
		$criteria->compare('grades_number', $this->grades_number);
		$criteria->compare('grades_total', $this->grades_total);
		$criteria->compare('last_modified_on', $this->last_modified_on, true);
		$criteria->compare('user_id', $this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function getGrade() {
		if ($this->grades_total == 0) {
			return 0;
		} else {
			return $this->grades_number / $this->grades_total;
		}
	}

}